<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="appid" prop="appid">
        <el-input
          v-model="queryParams.appid"
          placeholder="请输入appid"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出入账" prop="billType">
        <el-select v-model="queryParams.billType" placeholder="请选择出账/入账" clearable size="small">
          <el-option
            v-for="dict in billTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="交易类型" prop="transType">
        <el-select v-model="queryParams.transType" placeholder="请选择交易类型  1充值  2交易扣款" clearable size="small">
          <el-option
            v-for="dict in transTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <div>
          <el-divider></el-divider>
        </div>
      </el-col>

    </el-row>

    <el-table v-loading="loading" :data="billList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="appid" align="center" prop="appid" />
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="出账/入账" align="center" prop="billType" :formatter="billTypeFormat" />
      <el-table-column label="交易类型" align="center" prop="transType" :formatter="transTypeFormat"/>
      <el-table-column label="备注" align="center" prop="remark" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listBill, getBill, delBill, addBill, updateBill, exportBill } from "@/api/third/bill";

export default {
  name: "Bill",
  props: {
    accountId: {
      type: Number
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 第三方合伙人账户明细表格数据
      billList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 出账/入账字典
      billTypeOptions: [],
      transTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        accountId: this.accountId,
        partnerId: undefined,
        appid: undefined,
        amount: undefined,
        billType: undefined,
        transType: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        accountId: [
          { required: true, message: "账户id不能为空", trigger: "blur" }
        ],
        partnerId: [
          { required: true, message: "合伙人id不能为空", trigger: "blur" }
        ],
        amount: [
          { required: true, message: "金额不能为空", trigger: "blur" }
        ],
        billType: [
          { required: true, message: "出账/入账不能为空", trigger: "blur" }
        ],
        transType: [
          { required: true, message: "交易类型  1充值  2交易扣款不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("bill_type").then(response => {
      this.billTypeOptions = response.data;
    });
    this.getDicts("partner_trans_type").then(response => {
      this.transTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询第三方合伙人账户明细列表 */
    getList() {
      this.loading = true;
      listBill(this.queryParams).then(response => {
        this.billList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 出账/入账字典翻译
    billTypeFormat(row, column) {
      return this.selectDictLabel(this.billTypeOptions, row.billType);
    },

    transTypeFormat(row, column) {
      return this.selectDictLabel(this.transTypeOptions, row.transType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        accountId: undefined,
        partnerId: undefined,
        appid: undefined,
        amount: undefined,
        billType: undefined,
        transType: undefined,
        remark: undefined,
        createTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },

    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有第三方合伙人账户明细数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportBill(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
